package com.beagle.njddapp.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.PoiItem;
import com.beagle.njddapp.R;
import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.ZkBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.bean.owner.JIaYouZhanBean;
import com.beagle.njddapp.bean.owner.JiaYouCheBean;
import com.beagle.njddapp.bean.owner.NjAndOilBean;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.event.AddDeviceEvent;
import com.beagle.njddapp.event.AddOilCarEvent;
import com.beagle.njddapp.net.BaseResponse;
import com.beagle.njddapp.net.BaseResponseBean;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.activity.ChooseAeraActivity;
import com.beagle.njddapp.ui.activity.MainActivity;
import com.beagle.njddapp.ui.activity.SuggestActivity;
import com.beagle.njddapp.ui.presenter.MapPresenter;
import com.beagle.njddapp.ui.view.MapBaseView;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.weight.dialog.JiayouDIalog;
import com.beagle.njddapp.weight.dialog.NJOilPayDIalog;
import com.beagle.njddapp.weight.dialog.NjDetailDIalog;
import com.beagle.njddapp.weight.dialog.NjListAddOilDIalog;
import com.beagle.njddapp.weight.dialog.NjListDIalog;
import com.beagle.njddapp.weight.dialog.NjOilHandleDIalog;
import com.beagle.njddapp.weight.dialog.NjOilPjDIalog;
import com.beagle.njddapp.weight.dialog.NjOilWaitingDIalog;
import com.beagle.njddapp.weight.dialog.OilCarDIalog;
import com.beagle.njddapp.weight.dialog.OilStationDIalog;
import com.beagle.njddapp.weight.dialog.PayDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-13 19:12
 */
public class Mapfragment extends Fragment implements LocationSource, LocationManager.LocationChangeLisenter, MapBaseView, AMap.OnMarkerClickListener, NjListDIalog.onNjDialogItemClickLisenter, NjDetailDIalog.onNjJiaYouClaaBack, JiayouDIalog.JiaYouDialogHandleCallback, NjOilWaitingDIalog.NjOilOrderwaittingCallBack, NjListAddOilDIalog.onNjAddOilDialogItemClickLisenter, NjOilPjDIalog.NjOilPlCallBack, AMap.OnMapLoadedListener, PayDialog.PayCallback, NJOilPayDIalog.NjOilPayCallBack {

    @BindView(R.id.map)
    TextureMapView mMapView;
    private AMap aMap;
    //    @BindView(R.id.rl_oil)
//    RelativeLayout rlOil;
    @BindView(R.id.tv_number)
    TextView tvOilNumber;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_all_title)
    TextView tvAllTitle;
    @BindView(R.id.tv_all_count)
    TextView tvAllCount;
    @BindView(R.id.view_all)
    View viewAll;
    @BindView(R.id.tv_run_title)
    TextView tvRunTitle;
    @BindView(R.id.tv_run_count)
    TextView tvRunCount;
    @BindView(R.id.view_run)
    View viewRun;
    @BindView(R.id.tv_stop_title)
    TextView tvStopTitle;
    @BindView(R.id.tv_stop_count)
    TextView tvStopCount;
    @BindView(R.id.view_stop)
    View viewStop;
    @BindView(R.id.rl_nj_top)
    LinearLayout rlNjTop;
    private MainActivity activity;
    private int fromtype = -1; //是否来自加油
    private NjListAddOilDIalog njListAddOilDIalog;//农机列表dialog
    private NjListDIalog njListDIalog;//农机列表dialog
    private NjDetailDIalog njDetailDIalog;//农机详情dialog
    private NjOilWaitingDIalog waitingDIalog;//农机主-加油订单等待中dialog
    MyLocationStyle myLocationStyle;
    private LocationSource.OnLocationChangedListener mListener;
    private MapPresenter mapPresenter;
    private UiSettings mUiSettings;
    private List<NjBean> runList;
    private List<NjBean> stopList;
    private List<NjBean> allList;
    private double longitude = 0, latitude = 0;
    private boolean isFirstLocation;//是否第一次获取到位置
    private int color_blue, color_black, type = 1;
    private JiayouDIalog jiayouDIalog;//农机主-加油dialog
    private NjOilPjDIalog pjDIalog;//农机主-加油评价dialog
    private List<JiaYouCheBean> jiaYouCheBeans;
    private List<JIaYouZhanBean> jIaYouZhanBeanList;
    private boolean isMarkerClick;
    private OilCarDIalog oilCarDIalog;//加油车信息dialog
    private OilStationDIalog stationDIalog;//加油站信息dialog
    private NjOilHandleDIalog njOilHandleDIalog; //农机主加油订单-等待或加油已完成dialog
    private PayDialog choosePayDialog;//选择支付dialog
    private NJOilPayDIalog njOilPayDIalog;//支付dialog
    private HashMap<Marker, BaseResponseBean> hashMap;
    private PoiItem currentPos;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        activity = (MainActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocationManager.getInstance().addLisenter(this);
        mapPresenter = new MapPresenter();
        mapPresenter.registerView(this);
        color_black = getResources().getColor(R.color.color_black);
        color_blue = getResources().getColor(R.color.color_bule);
        runList = new ArrayList<>();
        stopList = new ArrayList<>();
        allList = new ArrayList<>();
        hashMap = new HashMap<>();
        EventBus.getDefault().register(this);
//        if (getArguments() != null) {
//            fromtype = getArguments().getInt("type", -1);
//        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        android.view.View view = inflater.inflate(getLayoutId(), null);
        ButterKnife.bind(this, view);
        mMapView.onCreate(savedInstanceState);
        return view;
    }

    public int getLayoutId() {
        return R.layout.fragment_map;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        aMap = mMapView.getMap();
        mUiSettings = aMap.getUiSettings();
        //设置地图的放缩级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
        aMap.setLocationSource(this);
        aMap.setOnMapLoadedListener(this);
        mUiSettings.setZoomControlsEnabled(false);
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）默认执行此种模式。
        myLocationStyle.showMyLocation(true);
        myLocationStyle.strokeColor(Color.TRANSPARENT);
        myLocationStyle.radiusFillColor(Color.TRANSPARENT);
//        if (UserService.getInstance().getRole()==UserService.ROLE_OIL){
//            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.maps_ic_gastanker));
//            myLocationStyle.myLocationIcon(bitmapDescriptor);
//        }else {
//            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.maps_ic_car));
//            myLocationStyle.myLocationIcon(bitmapDescriptor);
//        }
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setOnMarkerClickListener(this);

    }



    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        if (mapPresenter != null) {
            mapPresenter.destroy();
        }
        if (mMapView != null) {
            mMapView.onDestroy();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Override
    public void activate(LocationSource.OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
        mListener.onLocationChanged(LocationManager.getInstance().location);
    }

    @Override
    public void deactivate() {
        mListener = null;

    }

    @Override
    public void onLocationChanged(AMapLocation location) {
        if (!isFirstLocation) {
            isFirstLocation = true;
            if (UserService.getInstance().getRole() == UserService.ROLE_OWNER) {
//                mapPresenter.getDeviceListRun(1000, 1);
//                mapPresenter.getDeviceListStop(1000, 1);
                mapPresenter.getDeviceAll(1000, 1);
                mapPresenter.queryFillingStationList(1000, 1, "");
                mapPresenter.queryFillingCarList(1000, 1, "");
            } else {
                mapPresenter.myCar();
            }
        }

        if (longitude != location.getLongitude() || latitude != location.getLatitude()) {
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            mListener.onLocationChanged(location);
        }
    }

    @Override
    public void onError(String message) {
        if (!TextUtils.isEmpty(message)) {
            ToastUtils.showShort(message);
        }
    }

    @Override
    public void showOild(OilBean result) {
        if (result != null) {
            UserService.getInstance().setCarId(result.getCarNum());
//            rlOil.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    rlOil.setVisibility(View.VISIBLE);
//                    tvOilNumber.setText(result.getCarNum());
//                    tvName.setText(UserService.getInstance().getUserInfoBean().getUserName());
//                    tvLocation.setText(LocationManager.getInstance().getLocation());
//                }
//            }, 2000);

        }
    }

//    @Override
//    public void showRunNj(List<NjBean> results) {
//        rlNjTop.setVisibility(View.VISIBLE);
//        runList.clear();
//        runList.addAll(results);
//        tvRunCount.setText(results.size() + "");
//        if (isClick) {
//            isClick = false;
//            if (allList.size() > 1) {
//                if (njListDIalog == null) {
//                    njListDIalog = new NjListDIalog(getContext());
//                    njListDIalog.setOnNjDialogItemClickLisenter(this);
//                }
//                njListDIalog.show();
//                njListDIalog.setNjBeanList(runList);
//            }
//        }
//
//    }
//
//    @Override
//    public void showStopNj(List<NjBean> results) {
//        stopList.clear();
//        stopList.addAll(results);
//        tvStopCount.setText(results.size() + "");
//        if (isClick) {
//            isClick = false;
//            if (allList.size() > 1) {
//                if (njListDIalog == null) {
//                    njListDIalog = new NjListDIalog(getContext());
//                    njListDIalog.setOnNjDialogItemClickLisenter(this);
//                }
//                njListDIalog.show();
//                njListDIalog.setNjBeanList(stopList);
//            }
//        }
//    }

    @Override
    public void showAll(List<NjBean> results) {
        addNJMarker(results);
        rlNjTop.setVisibility(View.VISIBLE);
        allList.clear();
        stopList.clear();
        runList.clear();
        allList.addAll(results);
        tvAllCount.setText(allList.size() + "");
        for (NjBean njBean:results){
            if ("1".equals(njBean.getStatus())){
                runList.add(njBean);
            }else if ("2".equals(njBean.getStatus())){
                stopList.add(njBean);
            }
        }
        tvStopCount.setText(stopList.size() + "");
        tvRunCount.setText(runList.size() + "");
        if (fromtype == 1) {
            fromtype = -1;
            if (allList.size() > 0) {
                if (njListAddOilDIalog == null) {
                    njListAddOilDIalog = new NjListAddOilDIalog(getContext());
                    njListAddOilDIalog.setOnNjDialogItemClickLisenter(this);
                }
                njListAddOilDIalog.show();
                njListAddOilDIalog.setNjBeanList(allList);
            } else {
                ToastUtils.showShort("请先添加农机");
            }

        }
        if (isClick) {
            isClick = false;
            if (type==1){
                if (allList.size() > 1) {
                    if (njListDIalog == null) {
                        njListDIalog = new NjListDIalog(getContext());
                        njListDIalog.setOnNjDialogItemClickLisenter(this);
                    }
                    njListDIalog.show();
                    njListDIalog.setNjBeanList(allList);
                }
            }else if (type==2){
                if (runList.size() > 1) {
                    if (njListDIalog == null) {
                        njListDIalog = new NjListDIalog(getContext());
                        njListDIalog.setOnNjDialogItemClickLisenter(this);
                    }
                    njListDIalog.show();
                    njListDIalog.setNjBeanList(runList);
                }
            }else if (type==3){
                if (stopList.size() > 1) {
                    if (njListDIalog == null) {
                        njListDIalog = new NjListDIalog(getContext());
                        njListDIalog.setOnNjDialogItemClickLisenter(this);
                    }
                    njListDIalog.show();
                    njListDIalog.setNjBeanList(stopList);
                }
            }

        }
    }

    @Override
    public void showJyDialog(NjBean njBean) {
        if (jiayouDIalog == null) {
            jiayouDIalog = new JiayouDIalog(getContext());
            jiayouDIalog.setJiaYouDialogHandleCallback(this);
        }
        jiayouDIalog.show();
        jiayouDIalog.setNjBean(njBean);
    }

    @Override
    public void showPjDialog(NjAndOilBean njAndOilBean) {
        if (pjDIalog == null) {
            pjDIalog = new NjOilPjDIalog(getContext());
            pjDIalog.setNjOilPlCallBack(this);
        }
        pjDIalog.show();
        pjDIalog.update(njAndOilBean);
    }

    @Override
    public void pjSuccess() {
        ToastUtils.showShort("评价成功");
    }

    public void mainJiaYou() {
        fromtype = 1;
        mapPresenter.getDeviceAll(1000, 1);
    }

    @Override
    public void showJyc(List<JiaYouCheBean> results) {
        if (results == null || results.size() < 0) {
            return;
        }
        if (isMarkerClick) {
            isMarkerClick = false;
            if (oilCarDIalog == null) {
                oilCarDIalog = new OilCarDIalog(getContext());
            }
            oilCarDIalog.show();
            oilCarDIalog.update(results.get(0));
            return;
        }
        jiaYouCheBeans = results;
        addMarker(results);
    }

    @Override
    public void showJyz(List<JIaYouZhanBean> results) {
        if (results == null || results.size() < 0) {
            return;
        }
        if (isMarkerClick) {
            isMarkerClick = false;
            if (stationDIalog == null) {
                stationDIalog = new OilStationDIalog(getContext());
            }
            stationDIalog.show();
            stationDIalog.update(results.get(0));
            return;
        }
        jIaYouZhanBeanList = results;
        addJyzMarker(results);
    }

    public void addMarker(List<JiaYouCheBean> results) {
        for (JiaYouCheBean jiaYouCheBean : results) {
            MarkerOptions markerOption = new MarkerOptions();
            LatLng latLng = new LatLng(jiaYouCheBean.getLatitude(), jiaYouCheBean.getLongitude(), false);

            markerOption.position(latLng);
            markerOption.visible(true);
            markerOption.draggable(false);//设置Marker可拖动
            markerOption.setFlat(true);
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.maps_ic_gastanker));
            markerOption.icon(bitmapDescriptor);
            hashMap.put(aMap.addMarker(markerOption), jiaYouCheBean);
        }
    }
    public void addNJMarker(List<NjBean> results) {
        if (hashMap.size()>0){
            for (Marker marker : hashMap.keySet()) {
                marker.remove();
            }
        }
        hashMap.clear();
        for (NjBean njBean : results) {
            MarkerOptions markerOption = new MarkerOptions();
            LatLng latLng = new LatLng(njBean.getLatitude(), njBean.getLongitude(), false);

            markerOption.position(latLng);
            markerOption.visible(true);
            markerOption.draggable(false);//设置Marker可拖动
            markerOption.setFlat(true);
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.maps_ic_car_sel));
            markerOption.icon(bitmapDescriptor);
            hashMap.put(aMap.addMarker(markerOption), njBean);

        }
    }

    public void addJyzMarker(List<JIaYouZhanBean> results) {
        for (JIaYouZhanBean jIaYouZhanBean : results) {
            MarkerOptions markerOption = new MarkerOptions();
            LatLng latLng = new LatLng(jIaYouZhanBean.getLatitude(), jIaYouZhanBean.getLongitude(), false);

            markerOption.position(latLng);
            markerOption.visible(true);
            markerOption.draggable(false);//设置Marker可拖动
            markerOption.setFlat(true);
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.maps_ic_gasstation));
            markerOption.icon(bitmapDescriptor);
            hashMap.put(aMap.addMarker(markerOption), jIaYouZhanBean);

        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        BaseResponseBean baseResponseBean = hashMap.get(marker);
        if (baseResponseBean == null) {
            return false;
        }
        if (baseResponseBean instanceof JiaYouCheBean) {
            LatLng latLng = marker.getPosition();
            if (latLng != null && jiaYouCheBeans != null) {
                for (JiaYouCheBean jiaYouCheBean : jiaYouCheBeans) {
                    if (latLng.latitude == jiaYouCheBean.getLatitude() && latLng.longitude == jiaYouCheBean.getLongitude()) {
                        isMarkerClick = true;
                        mapPresenter.queryFillingCarList(1, 1, jiaYouCheBean.getCarId() + "");
                        break;
                    }
                }
            }
        } else if (baseResponseBean instanceof JIaYouZhanBean) {
            LatLng latLng = marker.getPosition();
            if (latLng != null && jIaYouZhanBeanList != null) {
                for (JIaYouZhanBean jIaYouZhanBean : jIaYouZhanBeanList) {
                    if (latLng.latitude == jIaYouZhanBean.getLatitude() && latLng.longitude == jIaYouZhanBean.getLongitude()) {
                        isMarkerClick = true;
                        mapPresenter.queryFillingStationList(1, 1, jIaYouZhanBean.getStationId() + "");
                        break;
                    }
                }
            }
        } else if (baseResponseBean instanceof NjBean) {
            LatLng latLng = marker.getPosition();
            if (latLng != null && allList != null) {
                for (NjBean njBean : allList) {
                    if (latLng.latitude == njBean.getLatitude() && latLng.longitude == njBean.getLongitude()) {
                        isMarkerClick = true;
                        showNjDetail(njBean);
                        break;
                    }
                }
            }
        }
        return true;
    }

    private void showNjDetail(NjBean njBean) {
        if (njDetailDIalog == null) {
            njDetailDIalog = new NjDetailDIalog(getContext());
            njDetailDIalog.setOnNjJiaYouClaaBack(this);
        }
        njDetailDIalog.show();
        njDetailDIalog.update(njBean);
    }

    private boolean isClick;

    @OnClick({R.id.rl_all, R.id.rl_run, R.id.rl_stop})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_all:
                isClick = true;
                mapPresenter.getDeviceAll(1000, 1);
                if (type == 1) {
                    return;
                }
                type = 1;
                tvAllTitle.setTextColor(color_blue);
                tvAllCount.setTextColor(color_blue);
                viewAll.setBackgroundColor(color_blue);
                tvRunTitle.setTextColor(color_black);
                tvRunCount.setTextColor(color_black);
                viewRun.setBackgroundColor(color_black);
                tvStopTitle.setTextColor(color_black);
                tvStopCount.setTextColor(color_black);
                viewStop.setBackgroundColor(color_black);
                break;
            case R.id.rl_run:
                isClick = true;
                mapPresenter.getDeviceAll(1000, 1);
                if (type == 2) {
                    return;
                }
                type = 2;
                tvRunTitle.setTextColor(color_blue);
                tvRunCount.setTextColor(color_blue);
                viewRun.setBackgroundColor(color_blue);
                tvAllTitle.setTextColor(color_black);
                tvAllCount.setTextColor(color_black);
                viewAll.setBackgroundColor(color_black);
                tvStopTitle.setTextColor(color_black);
                tvStopCount.setTextColor(color_black);
                viewStop.setBackgroundColor(color_black);
                break;
            case R.id.rl_stop:
                isClick = true;
                mapPresenter.getDeviceAll(1000, 1);
                if (type == 3) {
                    return;
                }
                type = 3;
                tvStopTitle.setTextColor(color_blue);
                tvStopCount.setTextColor(color_blue);
                viewStop.setBackgroundColor(color_blue);
                tvRunTitle.setTextColor(color_black);
                tvRunCount.setTextColor(color_black);
                viewRun.setBackgroundColor(color_black);
                tvAllTitle.setTextColor(color_black);
                tvAllCount.setTextColor(color_black);
                viewAll.setBackgroundColor(color_black);
                break;
        }
    }

    /**
     * 农机dialogue
     *
     * @param njBean
     */
    @Override
    public void onItemClick(NjBean njBean) {
        if (njDetailDIalog == null) {
            njDetailDIalog = new NjDetailDIalog(getContext());
            njDetailDIalog.setOnNjJiaYouClaaBack(this);
        }
        njDetailDIalog.show();
        njDetailDIalog.update(njBean);
    }

    /**
     * 农机dialog-加油
     *
     * @param njBean
     */
    @Override
    public void jiayou(NjBean njBean) {
        if (!TextUtils.isEmpty(njBean.getFuelOrderId()) && !"0".equals(njBean.getFuelOrderId())) {
            mapPresenter.getNjOilOrderDetail(njBean, njBean.getFuelOrderId());
        } else {
            if (jiayouDIalog == null) {
                jiayouDIalog = new JiayouDIalog(getContext());
                jiayouDIalog.setJiaYouDialogHandleCallback(this);
            }
            jiayouDIalog.show();
            jiayouDIalog.setNjBean(njBean);
        }
    }

    /**
     * 农机主-查询农机对应加油订单详情
     *
     * @param result
     * @param njBean
     */
    @Override
    public void showOrder(OilOrderBean result, NjBean njBean,ZkBean zkBean) {
        if (result == null) {
            if (jiayouDIalog == null) {
                jiayouDIalog = new JiayouDIalog(getContext());
                jiayouDIalog.setJiaYouDialogHandleCallback(this);
            }
            jiayouDIalog.show();
            jiayouDIalog.setNjBean(njBean);
            return;
        }
        if (result.isPayed()) {
            if (result.getVoteRate()!=0){
                //已评论
                if (jiayouDIalog == null) {
                    jiayouDIalog = new JiayouDIalog(getContext());
                    jiayouDIalog.setJiaYouDialogHandleCallback(this);
                }
                jiayouDIalog.show();
                jiayouDIalog.setNjBean(njBean);
            }else {
                //未评论
                if (pjDIalog==null){
                    pjDIalog=new NjOilPjDIalog(getContext());
                    pjDIalog.setNjOilPlCallBack(this);
                }
                pjDIalog.show();
                pjDIalog.update(result);
            }
            return;
        }
        if ("waiting".equals(result.getOrderState())) {
            if (waitingDIalog == null) {
                waitingDIalog = new NjOilWaitingDIalog(getContext());
                waitingDIalog.setNjOilOrderwaittingCallBack(this);
            }
            waitingDIalog.show();
            waitingDIalog.update(njBean, result);
        } else if ("canceled".equals(result.getOrderState()) || result.isPayed()) {
            if (jiayouDIalog == null) {
                jiayouDIalog = new JiayouDIalog(getContext());
                jiayouDIalog.setJiaYouDialogHandleCallback(this);
            }
            jiayouDIalog.show();
            jiayouDIalog.setNjBean(njBean);
        } else if ("confirmed".equals(result.getOrderState()) || "arrived".equals(result.getOrderState())) {
            if (njOilHandleDIalog == null) {
                njOilHandleDIalog = new NjOilHandleDIalog(getContext());
            }
            njOilHandleDIalog.show();
            njOilHandleDIalog.update(njBean, result);
        } else if ("finished".equals(result.getOrderState())) {
            if(zkBean==null){
                return;
            }
            //支付
            if (njOilPayDIalog == null) {
                njOilPayDIalog = new NJOilPayDIalog(getContext());
                njOilPayDIalog.setNjOilPayCallBack(this);

            }
            njOilPayDIalog.show();
            njOilPayDIalog.update(njBean, result,zkBean);
        }
    }

    /**
     * 农机主-加油dialog--发起加油
     *
     * @param njBean
     * @param phone
     * @param address
     * @param count
     */
    @Override
    public void JiaYouOk(NjBean njBean, String phone, String address, String count) {
        mapPresenter.jiayou(njBean, phone, address, count,currentPos);
    }

    @Override
    public void chooseAera() {
        startActivityForResult(new Intent(getContext(), ChooseAeraActivity.class),102);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==144&&data!=null){
            PoiItem poiItem=data.getParcelableExtra("pos");
            if (poiItem!=null){
                currentPos=poiItem;
                if (jiayouDIalog!=null&&jiayouDIalog.isShowing()){
                    jiayouDIalog.dirverUpdate(poiItem);
                }
            }
        }else if (resultCode==244&&data!=null){
            boolean isnow=data.getBooleanExtra("loc",false);
            if (isnow){
                if (jiayouDIalog!=null&&jiayouDIalog.isShowing()){
                    jiayouDIalog.dirverUpdateLocation();
                }
            }
        }
    }

    /**
     * 农机主-发起加油订单成功
     */
    @Override
    public void jiayouSuccess() {
        ToastUtils.showShort("加油订单已提交");
        mMapView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mapPresenter.getDeviceAll(1000, 1);
            }
        }, 500);


    }

    /**
     * 农机主-取消订单
     *
     * @param oilOrderBean
     */
    @Override
    public void cancleOilOrder(OilOrderBean oilOrderBean) {
        //取消订单
        mapPresenter.cancelOrder(oilOrderBean.getOrderId() + "");
    }


    @Override
    public void cancleSuccess() {
        ToastUtils.showShort("取消订单成功");
        mMapView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mapPresenter.getDeviceAll(1000, 1);
            }
        }, 500);


    }

    @Override
    public void paySuccess() {
        ToastUtils.showShort("操作成功,请在线下当面付款");
    }




    @Subscribe
    public void onAddDevice(AddDeviceEvent addDeviceEvent) {
        mMapView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mapPresenter.getDeviceAll(1000, 1);
            }
        }, 4000);

    }

    @Subscribe
    public void onOilCarAdd(AddOilCarEvent addOilCarEvent) {
        mMapView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mapPresenter.myCar();

            }
        }, 4500);
    }

    @Override
    public void onNjOilItemClick(NjBean njBean) {
        onItemClick(njBean);
    }

    @Override
    public void onJaAddOil(NjBean njBean) {
        jiayou(njBean);
    }

    @Override
    public void toSuggest() {
        startActivity(new Intent(getContext(), SuggestActivity.class));
    }

    @Override
    public void oilPl(long  id, int pl) {
        mapPresenter.pj(id, pl);
    }

    @Override
    public void onMapLoaded() {
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(LocationManager.getInstance().getLatitude(),LocationManager.getInstance().getLongitude()),15));
    }

    @Override
    public void aliPay(OilOrderBean bean,String price) {
        mapPresenter.aliPay(bean,price);
    }

    @Override
    public void wxPay(OilOrderBean bean,String price) {
        mapPresenter.wxPay(bean,price);
    }

    @Override
    public void choosePay(OilOrderBean oilOrderBean, String price) {
        if(choosePayDialog==null){
            choosePayDialog=new PayDialog(getActivity());
            choosePayDialog.setPayCallback(this);
        }
        choosePayDialog.show();
        choosePayDialog.setOilOrderBean(oilOrderBean,price);
    }
}
